package org.sky.image.controller;

import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.sky.image.common.pojo.Pages;
import org.sky.image.common.pojo.Result;
import org.sky.image.common.utils.CommonUtils;
import org.sky.image.pojo.Image;
import org.sky.image.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Date: 2019/3/4 15:05
 * @Description: 菜品图片分类
 */
@RestController
@RequestMapping("image")
@Slf4j
public class ImageController {


    @Autowired
    private ImageService imageService;


    @Value("${upload.path}")
    private String uploadPath;


    @Value("${upload.url}")
    private String uploadUrl;


    /**
     * 保存修改图片
     * @param image
     * @return
     */
    @PostMapping("saveOrUpdateImage")
    public ResponseEntity<Result> saveOrUpdateImage(Image image) {
        if (StringUtils.isBlank(image.getImages())) {
            return ResponseEntity.badRequest().body(Result.failMsg());
        }
        return this.imageService.saveOrUpdateImage(image);
    }



    /**
     * 删除图片
     * @param id
     * @return
     */
    @DeleteMapping("deleteImage")
    public ResponseEntity<Result> deleteImage(Integer id) {
        return this.imageService.deleteImage(id);
    }


    /**
     * 查询图片
     * @param image
     * @return
     */
    @GetMapping("selectImage")
    public ResponseEntity<Result> selectImage(Image image, Pages pages) {
        return this.imageService.selectImage(image, pages);
    }


    /**
     * 服务器上传文件
     *
     * @return
     */
    @PostMapping("upload")
    public ResponseEntity<Result> upload(MultipartFile file, String code) throws IOException {
        if (StringUtils.isBlank(code)) {
            return ResponseEntity.badRequest().body(Result.failMsg());
        }
        File f = new File(uploadPath);
        if (!f.exists()) {
            f.mkdirs();
            log.info("创建文件夹：{}", uploadPath);
        }
        String fileSuffix = CommonUtils.getFileSuffix(file.getOriginalFilename());
        String fileName = CommonUtils.getKey();
        fileName = fileName + fileSuffix;
        String filePath = uploadPath + code + "-" + fileName;
        File tmpFile = new File(filePath);
        file.transferTo(tmpFile);
        log.info("上传文件成功：{}" + fileName);
        return ResponseEntity.ok(Result.data(0, uploadUrl + code + "-" + fileName));
    }


    /**
     * 服务器删除文件
     *
     * @return
     */
    @DeleteMapping("deleteImg")
    public ResponseEntity<Result> deleteImg(String imgUrl) throws IOException {
        String img = imgUrl.replace(uploadUrl, "");
        File oldFile = new File(uploadPath + img);
        oldFile.delete();
        return ResponseEntity.ok(Result.successMsg());
    }


    /**
     * 识别图片信息
     * @return
     */
    @PostMapping("discernImage")
    public ResponseEntity<Result> discernImage(MultipartFile file, Integer id) throws IOException {
        return this.imageService.discernImage(file, id);
    }

}
